#include "mainwidget.h"
#include "ui_mainwidget.h"
#include <QTabWidget>
#include <QTabBar>
#include <QBitmap>
#include <QPainter>
#include <QMouseEvent>
#include <QDebug>

MainWidget::MainWidget(QWidget *parent, Person * l_user)
    : QWidget(parent)
    , ui(new Ui::MainWidget)
    , user(l_user)
{
    ui->setupUi(this);

    basicSettings();
    connectSettings();
}

MainWidget::~MainWidget()
{
    delete ui;
}

void MainWidget::basicSettings()
{
    //消除边框
    this->setWindowFlag(Qt::FramelessWindowHint);

    //设置默认页面-第一页
    ui->stackedWidget->setCurrentIndex(0);
    ui->btnPage1->setStyleSheet("QPushButton{font-size:22px;font-family:'黑体';background-color:rgb(240,240,240);color:rgb(110,54,255);border-radius:10px}");

    //设置菜单栏头像和昵称
    user->setPixmap(QPixmap(user->getPixmapPath()));
    ui->labPersonalPicture->setPixmap(PixmapToRound(user->getPixmap(), 29));
    ui->labPersonalPicture->setScaledContents(true); //图片填充
    ui->btnPersonalText->setText(user->getName());

    //实时换脸页面
    ui->horiFaceChange->addWidget(faceChan);

    //趣味换脸页面
    ui->horiInterestChange->addWidget(interestChan);

    //风格迁移页面
    ui->horiTransferChange->addWidget(transferChan);

    //人脸识别页面
    ui->horiDistinguishChange->addWidget(distingChan);

    //个人信息修改页面
    ui->vertUpdateInfo->addWidget(updateInfo);
}

void MainWidget::connectSettings()
{
    //关闭与最小化
    connect(ui->btnClose, &QPushButton::clicked, this, &QWidget::close);
    connect(ui->btnMinimize, &QPushButton::clicked, this, &QWidget::showMinimized);

    //个人菜单下拉框
    connect(ui->btnPersonalText, &QPushButton::clicked, this, &MainWidget::btnPersonalText_clicked);

    //信息修改页面的“保存”与“取消”
    connect(updateInfo, &updateInformation::sendData, this, &MainWidget::updateUser);
    connect(updateInfo, &updateInformation::restoreDefault, this, &MainWidget::restoreDefaultView);

    //等待窗口的展示与关闭
    connect(distingChan, &distinguishChange::showWait, this, &MainWidget::showWait);
    connect(distingChan, &distinguishChange::closeWait, this, &MainWidget::closeWait);
    connect(transferChan, &transferChange::showWait, this, &MainWidget::showWait);
    connect(transferChan, &transferChange::closeWait, this, &MainWidget::closeWait);
    connect(interestChan, &interestChange::showWait, this, &MainWidget::showWait);
    connect(interestChan, &interestChange::closeWait, this, &MainWidget::closeWait);
    connect(faceChan, &faceChange::showWait, this, &MainWidget::showWait);
    connect(faceChan, &faceChange::closeWait, this, &MainWidget::closeWait);

    //切换界面按钮
    QString basicStyleSheet = "QPushButton{font-size:22px;font-family:'黑体';background:transparent;color:rgb(240,240,240);border-radius:10px}"
                              "QPushButton:hover{background-color:rgb(87, 29, 203)}"
                              "QPushButton:pressed{background-color:rgb(69, 23, 162)}";
    QString selectedStyleSheet = "QPushButton{font-size:22px;font-family:'黑体';background-color:rgb(240,240,240);color:rgb(110,54,255);border-radius:10px}";

    connect(ui->btnPage1, &QPushButton::clicked, [=](){
        updateInfo->updateInfo();//刷新信息修改页内容，下同
        ui->stackedWidget->setCurrentIndex(0);
        ui->btnPage1->setStyleSheet(selectedStyleSheet);
        ui->btnPage2->setStyleSheet(basicStyleSheet);
        ui->btnPage3->setStyleSheet(basicStyleSheet);
        ui->btnPage4->setStyleSheet(basicStyleSheet);
    });
    connect(ui->btnPage2, &QPushButton::clicked, [=](){
        updateInfo->updateInfo();
        ui->stackedWidget->setCurrentIndex(1);
        ui->btnPage1->setStyleSheet(basicStyleSheet);
        ui->btnPage2->setStyleSheet(selectedStyleSheet);
        ui->btnPage3->setStyleSheet(basicStyleSheet);
        ui->btnPage4->setStyleSheet(basicStyleSheet);
    });
    connect(ui->btnPage3, &QPushButton::clicked, [=](){
        updateInfo->updateInfo();
        ui->stackedWidget->setCurrentIndex(2);
        ui->btnPage1->setStyleSheet(basicStyleSheet);
        ui->btnPage2->setStyleSheet(basicStyleSheet);
        ui->btnPage3->setStyleSheet(selectedStyleSheet);
        ui->btnPage4->setStyleSheet(basicStyleSheet);
    });
    connect(ui->btnPage4, &QPushButton::clicked, [=](){
        updateInfo->updateInfo();
        ui->stackedWidget->setCurrentIndex(3);
        ui->btnPage1->setStyleSheet(basicStyleSheet);
        ui->btnPage2->setStyleSheet(basicStyleSheet);
        ui->btnPage3->setStyleSheet(basicStyleSheet);
        ui->btnPage4->setStyleSheet(selectedStyleSheet);
    });

    //重启个人中心连接
    connect(personalSpace, &Personal_Space::remakePersonalSpace, this, &MainWidget::remakePersonalSpace);
}

void MainWidget::updateMenu()
{
    ui->labPersonalPicture->setPixmap(PixmapToRound(user->getPixmap(), 29));
    ui->btnPersonalText->setText(user->getName());
}

void MainWidget::mousePressEvent(QMouseEvent *event)
{
    if(event->y() < ui->menuBar->height() && event->buttons() & Qt::LeftButton)
    {
        address = event->globalPos();
    }
}

void MainWidget::mouseMoveEvent(QMouseEvent *event)
{
    if(event->y() < ui->menuBar->height() && event->buttons() & Qt::LeftButton)
    {
        int dx = event->globalX() - address.x();
        int dy = event->globalY() - address.y();
        this->move(this->x()+dx, this->y()+dy);
        address = event->globalPos();
    }
}

void MainWidget::mouseReleaseEvent(QMouseEvent *event)
{
    if(event->y() < ui->menuBar->height() && event->buttons() & Qt::LeftButton)
    {
        int dx = event->globalX() - address.x();
        int dy = event->globalY() - address.y();
        this->move(this->x()+dx, this->y()+dy);
        address = event->globalPos();
    }
}

void MainWidget::btnPersonalText_clicked()
{
    if(personCombo)
    {
        if(personCombo->isHidden())
        {
            personCombo->show();
        }
        else
        {
            personCombo->hide();
        }
    }
    else
    {
        personCombo = new personWidget(this);
        //返回登录界面，信号传信号
        connect(personCombo, &personWidget::backLogin, [this](){
            emit backLogin();
        });
        personCombo->setGeometry(875, 77, 200, 150);
        personCombo->raise();
        personCombo->show();
    }
}

void MainWidget::updateUser(Person *user)
{
    Q_UNUSED(user);

    //刷新主界面菜单栏
    updateMenu();
}

void MainWidget::restoreDefaultView()
{
    ui->stackedWidget->setCurrentIndex(0);
    ui->btnPage1->setStyleSheet("QPushButton{font-size:22px;font-family:'黑体';background-color:rgb(240,240,240);color:rgb(110,54,255);border-radius:10px}");
}

QPixmap MainWidget::PixmapToRound(const QPixmap &src, int radius)
{
    if(src.isNull())
        return QPixmap();
    QSize size(2*radius, 2*radius);
    QBitmap mask(size);
    QPainter painter(&mask);
    painter.setRenderHint(QPainter::Antialiasing);
    painter.setRenderHint(QPainter::SmoothPixmapTransform);
    painter.fillRect(0, 0, size.width(), size.height(), Qt::white);
    painter.setBrush(QColor(0, 0, 0));
    painter.drawRoundedRect(0, 0, size.width(), size.height(), 99, 99);
    QPixmap image = src.scaled(size);
    image.setMask(mask);

    return image;
}

void MainWidget::showWait()
{
    wait->show();
}

void MainWidget::closeWait()
{
    wait->close();
}

void MainWidget::remakePersonalSpace()
{
    delete personalSpace;
    personalSpace = new Personal_Space(nullptr, user);

    personalSpace->updateDate();
    personalSpace->show();

    connect(personalSpace, &Personal_Space::remakePersonalSpace, this, &MainWidget::remakePersonalSpace);
}
